package com.wxzhou.wangzhuan.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

public class AdvancedGsonPrettyPrint {

    // 完整的Gson配置
    private static final Gson GSON = new GsonBuilder()
            .setPrettyPrinting()              // 美化输出
            .serializeNulls()                 // 序列化null值
            .setDateFormat("yyyy-MM-dd HH:mm:ss") // 日期格式
            .disableHtmlEscaping()            // 禁用HTML转义
            .create();

    // 1. 直接美化对象
    public static String prettyPrint(Object object) {
        return GSON.toJson(object);
    }

    // 2. 美化已有的JSON字符串
    public static String prettyPrintJson(String jsonString) {
        try {
            return GSON.toJson(JsonParser.parseString(jsonString));
        } catch (Exception e) {
            return "Invalid JSON: " + jsonString;
        }
    }

    // 3. 带缩进控制的美化
    public static String prettyPrintWithIndent(Object object, String indent) {
        Gson customGson = new GsonBuilder()
                .setPrettyPrinting()
                .serializeNulls()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .disableHtmlEscaping()
                .create();

        return customGson.toJson(object);
    }
}